﻿#include <iostream>
#include <vector>
#include <queue>
using namespace std;

class Solution
{
    bool vis[51][51];
    int m, n;
    int dx[4] = { 0,0,1,-1 };
    int dy[4] = { 1,-1,0,0 };
    int count = 0, ret = 0;
public:
    // 695. 岛屿的最大面积
    int maxAreaOfIsland(vector<vector<int>>& grid)
    {
        m = grid.size(), n = grid[0].size();
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (!vis[i][j] && grid[i][j] == 1)
                {
                    count = 0;
                    bfs(grid, i, j);
                    ret = max(ret, count);
                }
            }
        }
        return ret;
    }

    void bfs(vector<vector<int>>& grid, int i, int j)
    {
        queue<pair<int, int>> q;
        q.push({ i,j });
        vis[i][j] = true;

        while (q.size())
        {
            pair<int, int> p = q.front();
            q.pop();
            count++;
            int a = p.first, b = p.second;

            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 1)
                {
                    vis[x][y] = true;
                    q.push({ x,y });
                }
            }
        }
    }

    // 130. 被围绕的区域
    void solve(vector<vector<char>>& board)
    {
        m = board.size(), n = board[0].size();

        for (int i = 0; i < m; i++)
        {
            if (board[i][0] == 'O')
                bfs(board, i, 0);
            if (board[i][n - 1] == 'O')
                bfs(board, i, n - 1);
        }

        for (int j = 0; j < n; j++)
        {
            if (board[0][j] == 'O')
                bfs(board, 0, j);
            if (board[m - 1][j] == 'O')
                bfs(board, m - 1, j);
        }

        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if (board[i][j] == '.')
                    board[i][j] = 'O';
                else if (board[i][j] == 'O')
                    board[i][j] = 'X';
            }
        }
    }

    void bfs(vector<vector<char>>& board, int i, int j)
    {
        queue<pair<int, int>> q;
        q.push({ i,j });
        board[i][j] = '.';

        while (q.size())
        {
            pair<int, int> p = q.front();
            q.pop();
            int a = p.first, b = p.second;

            for (int k = 0; k < 4; k++)
            {
                int x = a + dx[k], y = b + dy[k];
                if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O')
                {
                    board[x][y] = '.';
                    q.push({ x,y });
                }
            }
        }
    }
};

int main()
{
    /*vector<vector<int>> v{ {0,0,1,0,0,0,0,1,0,0,0,0,0} ,
        {0,0,0,0,0,0,0,1,1,1,0,0,0},{0,1,1,0,1,0,0,0,0,0,0,0,0},
        {0,1,0,0,1,1,0,0,1,0,1,0,0},{0,1,0,0,1,1,0,0,1,1,1,0,0},
        {0,0,0,0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,1,1,1,0,0,0},
        {0,0,0,0,0,0,0,1,1,0,0,0,0} };
    cout << Solution().maxAreaOfIsland(v) << endl;*/

    vector<vector<char>> v{ {'X','X','X','X'} ,{'X','O','O','X'},
        {'X','X','O','X'},{'X','O','X','X'} };
    for (int i = 0; i < v.size(); i++)
    {
        for (int j = 0; j < v[0].size(); j++)
        {
            cout << v[i][j] << " ";
        }
        cout << endl;
    }
    Solution().solve(v);
    cout << endl;
    for (int i = 0; i < v.size(); i++)
    {
        for (int j = 0; j < v[0].size(); j++)
        {
            cout << v[i][j] << " ";
        }
        cout << endl;
    }

    return 0;
}
